<!--
* @author wn
* @date 2022/04/19 09:50:47
* @description: 头部组件的 购物车
!-->
<template>
	<div class="app-header-cart">
		<!-- 购物车图标 -->
		<router-link to="/cart" class="curr">
			<i class="iconfont icon-cart"></i>
			<em> {{ $store.getters['cart/validTotal'] }} </em>
		</router-link>
		<!-- 购物车弹层 没有商品不显示 购物车界面不显示-->
		<div
			class="layer"
			v-if="$store.getters['cart/validTotal'] && $route.path !== '/cart'"
		>
			<div class="list">
				<div
					class="item"
					v-for="good in $store.getters['cart/validList']"
					:key="good.skuId"
				>
					<router-link :to="`/product/${good.id}`">
						<img :src="good.picture" alt="" />
						<div class="center">
							<p class="name ellipsis-2">{{ good.name }}</p>
							<p class="attr ellipsis">{{ good.attrsText }}</p>
						</div>
						<div class="right">
							<p class="price">&yen;{{ good.price }}</p>
							<p class="count">x{{ good.count }}</p>
						</div>
					</router-link>
					<i
						@click="deleteCart(good.skuId)"
						class="iconfont icon-close-new"
					></i>
				</div>
			</div>
			<div class="footer">
				<div class="total">
					<p>共 {{ $store.getters['cart/validTotalCount'] }} 件商品</p>
					<p>&yen;{{ $store.getters['cart/validTotalPrice'] }}</p>
				</div>
				<xtx-button @click="$router.push('/cart')" type="plain">
					去购物车结算
				</xtx-button>
			</div>
		</div>
	</div>
</template>
<script>
// import { watch } from 'vue'
import { useStore } from 'vuex'
import Message from './library/message'
// import { useRoute } from 'vue-router'
export default {
	name: 'AppHeaderCart',
	setup() {
		// 未登录：获取最新库存信息(时间久了本地购物车 部分会失效改变)
		// 已登录：获取用户购物车列表
		const store = useStore()
		store
			.dispatch('cart/updateCart')
			.then(v => Message({ message: v || '更新购物车数据空' }))
		// 删除购物车 某个商品 skuId
		const deleteCart = skuId => store.dispatch('cart/deleteCart', skuId)
		return { deleteCart }
	},
}
</script>
<style scoped lang="less">
.app-header-cart {
	// 加限制
	position: relative;
	width: 50px;
	.curr {
		display: block;
		position: relative;
		height: 32px;
		line-height: 32px;
		text-align: center;
		.icon-cart {
			font-size: 22px;
		}
		em {
			position: absolute;
			top: 0;
			right: 0;
			font-style: normal;
			padding: 1px 6px;
			line-height: 1;
			background-color: @helpColor;
			color: #fff;
			font-size: 12px;
			border-radius: 10px;
			font-family: Arial;
		}
	}
	&:hover {
		.layer {
			opacity: 1;
			transform: none;
		}
	}
	// 购物车弹层
	.layer {
		position: absolute;
		top: 50px;
		right: 0;
		width: 400px;
		height: 400px;
		border-radius: 4px;
		padding-top: 10px;
		background-color: #fff;
		box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
		z-index: 666;
		// 隐藏  鼠标悬浮  动画 显示  谁做动画给谁加
		opacity: 0;
		// 向上 移动200 宽度不变  高度为0
		transform: translateY(-200px) scale(1, 0);
		transition: all 0.5s 0.2s;
		// 向上三角  注意阴影方向 配合弹框
		// scale(0.6, 1) x轴缩放 使三角夹角变小
		&::before {
			position: absolute;
			right: 14px;
			top: -10px;
			content: '';
			width: 20px;
			height: 20px;
			background: #fff;
			box-shadow: -3px -3px 5px rgba(0, 0, 0, 0.1);
			transform: scale(0.6, 1) rotate(45deg);
		}
		.list {
			height: 310px;
			// 自动滚动
			overflow: auto;
			padding: 0 10px;
			// 滚动条样式
			&::-webkit-scrollbar {
				width: 10px;
				height: 10px;
			}
			// 滚动条轨道
			&::-webkit-scrollbar-track {
				background: #f8f8f8;
				border-radius: 2px;
			}
			// 滚动条
			&::-webkit-scrollbar-thumb {
				background: #eee;
				border-radius: 10px;
			}
			// 滚动条悬浮
			&::-webkit-scrollbar-thumb:hover {
				background: #ccc;
			}
			.item {
				position: relative;
				border-bottom: 1px solid #f5f5f5;
				padding: 10px 0;
				a {
					display: flex;
					align-items: center;
					img {
						height: 80px;
						width: 80px;
					}
					.center {
						width: 200px;
						padding: 0 10px;
						.name {
							font-size: 16px;
						}
						.attr {
							color: #999;
							margin-top: 5px;
						}
					}
					.right {
						width: 100px;
						padding-right: 20px;
						text-align: center;
						.price {
							font-size: 16px;
							color: @priceColor;
						}
						.count {
							color: #999;
							margin-top: 5px;
							font-size: 16px;
						}
					}
				}
				i {
					position: absolute;
					bottom: 38px;
					right: 0;
					opacity: 0;
					color: #666;
					transition: all 0.5s;
				}
				&:hover {
					i {
						opacity: 1;
						cursor: pointer;
					}
				}
			}
		}
		.footer {
			display: flex;
			justify-content: space-between;
			align-items: center;
			position: absolute;
			left: 0;
			bottom: 0;
			width: 100%;
			height: 70px;
			padding: 0 10px;
			background: #f8f8f8;
			.total {
				padding-left: 10px;
				p {
					color: #999;
					~ p {
						color: @priceColor;
						font-size: 18px;
					}
				}
			}
		}
	}
}
</style>
